#include <iostream>
#include <unistd.h>
#include <fcntl.h>

void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if(fl < 0)
    {
        perror("fcntl");
        exit(1);
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}

int main()
{
    SetNonBlock(0);
    while(true)
    {
        char buffer[1024];
        int n = read(0, buffer, sizeof buffer - 1);
        if(n > 0)
        {
            buffer[n] = 0;
            std::cout << "echo#" << buffer << std::endl;
        }
        else if(n == 0)
        {
            std::cout << "end..." << std::endl;
            break;
        }
        else
        {
            if(errno == EWOULDBLOCK)
            {
                std::cout << "数据没有就绪 errno: " << errno <<  std::endl;
            }
            else if(errno == EINTR)
            {
                std::cout << "被信号中断... errno: " << errno << std::endl; 
            }
            else
            {
                std::cout << "read error" << std::endl;
                break;
            }
        }
        sleep(1);
    }
}